public class Solution965 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isUnivalTree(TreeNode root) {
        return dfs(root,root.val);
    }

    private boolean dfs(TreeNode root, int val) {
        if (root==null){
            return true;
        }
        if (root.val!=val){
            return false;
        }
        return dfs(root.left,val)&&dfs(root.right,val);
    }

    public static void main(String[] args) {
        System.out.println(new Solution965().isUnivalTree(new TreeNode(1,new TreeNode(1,new TreeNode(1),new TreeNode(1)),new TreeNode(1,null,new TreeNode(1)))));
    }
}
